﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UpgradeButton : MonoBehaviour {

    private GameObject applyButton = null;
    private GameObject unlockButton = null;

	// Use this for initialization
	void Start () {

        for (int i = 0; i < this.transform.parent.childCount; i++)
        {
            if (this.transform.parent.GetChild(i).gameObject.name.Contains("Apply"))
                applyButton = this.transform.parent.GetChild(i).gameObject;
            else if (this.transform.parent.GetChild(i).gameObject.name.Contains("Unlock"))
                unlockButton = this.transform.parent.GetChild(i).gameObject;
        }
        //if (int.Parse(GetComponentInChildren<Text>().text) > UpgradeController.Instance.GetHighestChoosableLevel(this.transform.parent.gameObject))
            //Debug.Log("Button " + GetComponentInChildren<Text>().text + " deaktiviert");
       // else
            //Debug.Log("Button " + GetComponentInChildren<Text>().text + " aktiviert");

	}
	
	// Update is called once per frame
    //void Update () {

    //}

    public void CheckButton(GameObject selectedButton = null)
    {
        if (selectedButton == null)
        {
            UpgradeController.Instance.SetSelectedButton(gameObject);
            Debug.Log("Level Button: " + int.Parse(GetComponentInChildren<Text>().text));
            Debug.Log("Level Current: " + UpgradeController.Instance.GetCurrentLevel(this.transform.parent.gameObject));

            if (UpgradeController.Instance.GetCurrentLevel(this.transform.parent.gameObject) == int.Parse(GetComponentInChildren<Text>().text))
            {
                applyButton.SetActive(false);
                unlockButton.SetActive(false);
            }
            else if (int.Parse(GetComponentInChildren<Text>().text) <= UpgradeController.Instance.GetHighestChoosableLevel(this.transform.parent.gameObject))
            {
                applyButton.SetActive(true);
                unlockButton.SetActive(false);
            }

            Debug.Log("Level Button: " + int.Parse(GetComponentInChildren<Text>().text));
            Debug.Log("Level Highest: " + UpgradeController.Instance.GetHighestChoosableLevel(this.transform.parent.gameObject));
        }
        else
        {
            if (UpgradeController.Instance.GetCurrentLevel(selectedButton.transform.parent.gameObject) == int.Parse(selectedButton.GetComponentInChildren<Text>().text))
            {
                applyButton.SetActive(false);
                unlockButton.SetActive(false);
            }
            else if (int.Parse(selectedButton.GetComponentInChildren<Text>().text) <= UpgradeController.Instance.GetHighestChoosableLevel(selectedButton.transform.parent.gameObject))
            {
                applyButton.SetActive(true);
                unlockButton.SetActive(false);
            }
        }

    }

    public void ApplyLevel()
    {
        UpgradeController.Instance.ApplyLevel();
        CheckButton(UpgradeController.Instance.GetSelectedButton()); //TODO: möglich, dass UpgradeController.Instance.ApplyLevel(); noch nicht fertig ist
                                                                        // aber checkbutton schon vorher ausgeführt wird -> Generics oder wie die heißen, oder asynchron?

    }
}
